<?php


define('IN_SCRIPT',1);

/* Get all the required files and functions */
require_once('hd_settings.inc.php');
require_once('language/'.$hd_settings['language'].'.inc.php');
require_once('inc/common.inc.php');
hd_session_start();

$trackingID=strtoupper(hd_input($_GET['track'],$hdlang['trackID_not_found']));

/* Connect to database */
require_once('inc/database.inc.php');
hd_dbConnect() or hd_error("$hdlang[cant_connect_db] $hdlang[contact_webmsater] $hd_settings[webmaster_mail]!");

/* Get request info */
$sql = "SELECT * FROM `hd_requests` WHERE `requestid`='$trackingID' LIMIT 1";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
if (hd_dbNumRows($result) != 1) {hd_error($hdlang['request_not_found']);}
$request = hd_dbFetchAssoc($result);

/* Get residence name and ID */
$sql = "SELECT * FROM `hd_residences` WHERE `id`=$request[residence] LIMIT 1";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
/* If this residence has been deleted use the default residence with ID 1 */
    if (hd_dbNumRows($result) != 1)
    {
        $sql = "SELECT * FROM `hd_residences` WHERE `id`=1 LIMIT 1";
        $result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
    }
$residence = hd_dbFetchAssoc($result);

/* Get replies */
$sql = "SELECT * FROM `hd_replies` WHERE `replyto`='$request[id]' ORDER BY `id` ASC";
$result = hd_dbQuery($sql) or hd_error("$hdlang[cant_sql]: $sql</p><p>$hdlang[mysql_said]:<br>".mysql_error()."</p><p>$hdlang[contact_webmsater] $hd_settings[webmaster_mail]");
$replies = hd_dbNumRows($result);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title><?php echo $hd_settings['hd_title']; ?></title>
<meta content="text/html; charset=<?php echo $hdlang['ENCODING']; ?>">
<style type="text/css">
body,p,td {
    color : black;
    font-family : Verdana, Geneva, Arial, Helvetica, sans-serif;
    font-size : <?php echo $hd_settings['print_font_size']; ?>px;
}
</style>
</head>
<body onLoad="window.print()">


<?php
echo <<<EOC
<p>$hdlang[subject]: <b>$request[subject]</b><br>

$hdlang[trackID]: $trackingID<br>
$hdlang[request_status]:

EOC;

$random=rand(10000,99999);

    switch ($request['status']) {
    case 0:
        $request['status']=$hdlang['open'];
        break;
    case 1:
        $request['status']=$hdlang['wait_staff_reply'];
        break;
    case 2:
        $request['status']=$hdlang['wait_cust_reply'];
        break;
    default:
        $request['status']=$hdlang['closed'];
    }

        if ($request['lastreplier']) {$request['lastreplier'] = $hdlang['staff'];}
        else {$request['lastreplier'] = $hdlang['Student'];}

echo <<<EOC
$request[status]<br>
$hdlang[created_on]: $request[dt]<br>
$hdlang[last_update]: $request[lastchange]<br>
$hdlang[last_replier]: $request[lastreplier]<br>
$hdlang[residence]: $residence[name]<br>
$hdlang[replies]: $replies<br>
$hdlang[priority]:

EOC;
    if ($request['priority']==1) {echo "<b>$hdlang[high]</b>";}
    elseif ($request['priority']==2) {echo "$hdlang[medium]";}
    else {echo "$hdlang[low]";}

echo <<<EOC
<hr>
$hdlang[date]: $request[dt]<br>
$hdlang[name]: $request[name]<br>
$hdlang[email]: $request[email]<br>
$hdlang[ip]: $request[ip]<br>

EOC;

/* custom fields */
if ($hd_settings['use_custom']) {

    foreach ($hd_settings['custom_fields'] as $k=>$v) {
        if ($v['use']) {echo $v['name'].': '.$request[$k].'<br>';}
    }

}

echo <<<EOC
<b>$hdlang[message]:</b><br>
$request[message]

<hr>

EOC;

while ($reply = hd_dbFetchAssoc($result))
{
echo <<<EOC
$hdlang[date]: $reply[dt]<br>
$hdlang[name]: $reply[name]<br>
<b>$hdlang[message]:</b><br>
$reply[message]

<hr>

EOC;
}

echo $hdlang['end_request'];
?>
</p>

</body>
</html>
